package com.xidian.zhiancloud.service.luling.RiskIdentificationPart;

import com.xidian.zhiancloud.dao.RiskIdentEvalStatusMapper;
import com.xidian.zhiancloud.enums.GeneralResponseEnums;
import com.xidian.zhiancloud.exception.CustomizeException;
import com.xidian.zhiancloud.model.RiskIdentEvalStatus;
import com.xidian.zhiancloud.model.RiskIdentEvalStatusExample;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Date;
import java.util.List;

import static com.xidian.zhiancloud.util.tools.ObjectUtil.isEmpty;

@Service
public class RiskIdentEvalStatusService {
    @Autowired
    private RiskIdentEvalStatusMapper riskIdentEvalStatusMapper;
    //添加
    @Transactional(rollbackFor = {RuntimeException.class, Error.class, CustomizeException.class})
    public void add(RiskIdentEvalStatus riskIdentEvalStatus) {
        if(!isEmpty(riskIdentEvalStatus)){
            riskIdentEvalStatusMapper.insert(riskIdentEvalStatus);
        }else{
            throw new CustomizeException(GeneralResponseEnums.SYSTEM_ERROR);
        }
    }
    //删除
    @Transactional(rollbackFor = {RuntimeException.class, Error.class, CustomizeException.class})
    public void deletebyid(Long id) {
        riskIdentEvalStatusMapper.deleteByPrimaryKey(id);
    }
    public List<RiskIdentEvalStatus> getListPlan(int current, int size) {
        RiskIdentEvalStatusExample examTypeExample = new RiskIdentEvalStatusExample();
        return riskIdentEvalStatusMapper.selectByExampleWithRowbounds(examTypeExample, new RowBounds(current, size));
    }
    //通过状态查询
    public List<RiskIdentEvalStatus> getById(String state) {
        RiskIdentEvalStatusExample riskIdentEvalStatusExample = new RiskIdentEvalStatusExample();
        RiskIdentEvalStatusExample.Criteria criteria = riskIdentEvalStatusExample.createCriteria();
        criteria.andStateEqualTo(state);
        List<RiskIdentEvalStatus> riskIdentEvalStatusList = riskIdentEvalStatusMapper.selectByExample(riskIdentEvalStatusExample);
        if (riskIdentEvalStatusList == null) {
            throw new CustomizeException(GeneralResponseEnums.SYSTEM_ERROR);
        }
        return riskIdentEvalStatusList;
    }
    //关键字搜索
    public Object getList(String riskName, Date startTime, Date deadlineIden, Date deadlineEval, String type, String key, int current, int size) {

        RiskIdentEvalStatusExample riskIdentEvalStatusExample = new RiskIdentEvalStatusExample();
        RiskIdentEvalStatusExample.Criteria criteria1 = riskIdentEvalStatusExample.createCriteria();
        RiskIdentEvalStatusExample.Criteria criteria2 = riskIdentEvalStatusExample.createCriteria();
        RiskIdentEvalStatusExample.Criteria criteria3 = riskIdentEvalStatusExample.createCriteria();
        criteria1.andDeadlineEvalEqualTo(deadlineEval);
        criteria2.andDeadlineEvalEqualTo(deadlineEval);
        criteria3.andDeadlineEvalEqualTo(deadlineEval);
        if (type != null) {
            criteria1.andTypeEqualTo(type);
            criteria2.andTypeEqualTo(type);
            criteria3.andTypeEqualTo(type);
        }
        if (deadlineIden != null) {
            criteria1.andDeadlineIdenEqualTo(deadlineIden);
            criteria2.andDeadlineIdenEqualTo(deadlineIden);
            criteria3.andDeadlineIdenEqualTo(deadlineIden);
        }
        if (key!= null && !"".equals(key)) {
//            criteria1.andDeadlineIdenEqualTo('%' + key + '%');
            criteria2.andTypeEqualTo('%' + key + '%');
//            criteria3.andDeadlineEvalEqualTo('%' + key + '%');
            riskIdentEvalStatusExample.or(criteria2);
            riskIdentEvalStatusExample.or(criteria3);
        }
        return riskIdentEvalStatusMapper.selectByExampleWithRowbounds(riskIdentEvalStatusExample, new RowBounds(current, size));
    }

}

